﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;

namespace Twittervice.api
{
    /// <summary>
    /// This class implements basic logging functionality. Implements the 
    /// singleton patter.
    /// </summary>
    public class Logger
    {
        // Instance of the Logger Class
        private static Logger INSTANCE = new Logger("Twittervice");

        public static Logger getInstance()
        {
            return INSTANCE;
        }

        // The level of a log message
        public enum Level
        {
            INFO,
            WARNING,
            SEVERE
        }

        private string logFileName;
        private string fullLogFileName;

        /// <summary>
        /// Constructor of the Logger class
        /// </summary>
        /// <param name="fileName">The basic name of the log file</param>
        public Logger(string fileName)
        {
            logFileName = fileName;

            // Generate the fully qualified file name
            fullLogFileName = GenerateDefaultLogFileName(fileName);
        }

        /// <summary>
        /// Pass in the fully qualified name of the log file you want to write to
        /// and the message to write
        /// </summary>
        /// <param name="LogPath"></param>
        /// <param name="Message"></param>
        public void Log(Level level, string Message)
        {
            try
            {
                using (StreamWriter s = File.AppendText(fullLogFileName))
                {
                    String levelStr = Enum.GetName(typeof(Level), level);
                    String time = DateTime.Now.ToString();
                    s.WriteLine(levelStr + "\t" + time + "\t" + Message);
                    s.WriteLine();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

        }

        /// <summary>
        /// Log an INFO message
        /// </summary>
        /// <param name="message">The message text</param>
        public void LogInfo(string message)
        {
            Log(Level.INFO, message);
        }

        /// <summary>
        /// Log a WARNING message
        /// </summary>
        /// <param name="message">The message text</param>
        public void LogWarning(string message)
        {
            Log(Level.WARNING, message);
        }

        /// <summary>
        /// Log a SEVERE message
        /// </summary>
        /// <param name="message">The message text</param>
        public void LogSevere(string message)
        {
            Log(Level.SEVERE, message);
        }

        /// <summary>
        /// Writes a message to the application event log
        /// /// </summary>
        /// <param name="Source">Source is the source of the message ususally you will want this to be the application name</param>
        /// <param name="Message">message to be written</param>
        /// <param name="EntryType">the entry type to use to categorize the message like for exmaple error or information</param>
        public static void WriteToEventLog(string Source, string Message, System.Diagnostics.EventLogEntryType EntryType)
        {
            try
            {
                if (!EventLog.SourceExists(Source))
                {
                    EventLog.CreateEventSource(Source, "Application");
                }

                EventLog.WriteEntry(Source, Message, EntryType);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        // Method used to generate the full log file name and path
        private static string GenerateDefaultLogFileName(string BaseFileName)
        {
            //Append the Date/Time to the Log File Name
            string monthString = string.Format("{0:d2}", DateTime.Now.Month);
            string dayString = string.Format("{0:d2}", DateTime.Now.Day);

            return AppDomain.CurrentDomain.BaseDirectory + "log" +
                "\\" + BaseFileName + "_" + DateTime.Now.Year + "_" +
                monthString + "_" + dayString + "_" + DateTime.Now.Hour +
                DateTime.Now.Minute + DateTime.Now.Second + ".log";
        }
    }
}
